//
//  HHLoginViewController.m
//  MacWeb
//
//  Created by 小贝 on 16/3/14.
//  Copyright © 2016年 Boye. All rights reserved.
//

#import "HHLoginViewController.h"
#import "QuickLoginViewController.h"
#import "NSButton+TextColor.h"
@interface HHLoginViewController ()<NSTextFieldDelegate,NSTextDelegate>
//属性
@property (weak) IBOutlet NSSecureTextField *passwordTF;//密码
@property (weak) IBOutlet NSTextField *userNameTF;//账号
@property (weak) IBOutlet NSButton *quickBtn;//快速登录按钮
@property (weak) IBOutlet NSButton *loginBtn;//登录按钮
@property (weak) IBOutlet NSButton *forgetBtn;//忘记密码

//事件
- (IBAction)forgetAction:(NSButton *)sender;//点击忘记密码
- (IBAction)quickAction:(id)sender;//点击快速登录
- (IBAction)loginAction:(id)sender;//点击登录
@end
@implementation HHLoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    [NSButton setTextColor:self.forgetBtn andColor:[NSColor whiteColor]];
    self.passwordTF.delegate = self;
   
    [self changeCursor];
}
- (void)controlTextDidChange:(NSNotification *)obj{
    NSLog(@"%@",obj);
    
}
- (void)textDidChange:(NSNotification *)notification{
    NSLog(@"%@",notification);
}

#pragma mark---点击登录按钮(普通登录)
- (IBAction)loginAction:(id)sender {
    if ([NSString isStringNull:self.userNameTF.stringValue]) {
        [self showMessage:@"账号不能为空" andXoffset:self.userNameTF.bounds.size.width yOffset:0 andColor:[NSColor clearColor]];
        return;
    }
    if ([NSString isStringNull:self.passwordTF.stringValue]) {
        float yOffset_pwd = self.userNameTF.frame.origin.y - self.passwordTF.frame.origin.y ;
        [self showMessage:@"密码不能为空" andXoffset:self.passwordTF.bounds.size.width yOffset:yOffset_pwd andColor:[NSColor clearColor]];
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@?account=%@&password=%@&date=20160101&bookid=1",CommonLoginRooTPath,self.userNameTF.stringValue,self.passwordTF.stringValue];
    NSString *urlStr = [NSString URLEncodedString:url];
    [HHNetRequest requestWithGet:urlStr parmeter:nil successBlock:^(id resultValue) {
        NSString *result = [resultValue objectForKey:@"loginRst"];
        if ([result isEqualToString:@"0"]) {
            ViewController *mainVC = [self.storyboard instantiateControllerWithIdentifier:@"MainVC"];
            mainVC.userName = self.userNameTF.stringValue;
            mainVC.password = self.passwordTF.stringValue;
            mainVC.type = @"login";
            [self setContentVC:mainVC];
        }else{
            [self showMessage:@"用户名或者密码错误" andXoffset:0 yOffset:100.f andColor:nil];
        }
        
    } failBlock:^(id resultValue) {
        NSLog(@"失败");
    }];
}
#pragma mark-----点击快速登录
- (IBAction)quickAction:(NSButton *)sender {
    QuickLoginViewController *quickVC = [self.storyboard instantiateControllerWithIdentifier:@"quickVC"];
    [self setContentVC:quickVC];
}
#pragma mark--点击忘记密码
- (IBAction)forgetAction:(NSButton *)sender {
    ViewController *vc = [self.storyboard instantiateControllerWithIdentifier:@"MainVC"];
    vc.type = @"forget";
    [self setContentVC:vc];
}
#pragma mark--切换视图控制器并全屏显示
- (void)setContentVC:(NSViewController *) vc{
    [[NSApp mainWindow] setContentViewController:vc];
    float width = [NSScreen mainScreen].frame.size.width;
    float height =[NSScreen mainScreen].frame.size.height;
    [[NSApp mainWindow] setContentSize:NSMakeSize(width, height)];
}
#pragma mark--提示框
- (void) showMessage:(NSString *) message andXoffset:(float) xOffset yOffset:(float) yOffset andColor:(NSColor *) color{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.color = color;
    hud.margin = 8.f;
    [hud hide:YES afterDelay:2];
    hud.labelText = message;
    hud.xOffset = xOffset;
    hud.yOffset = yOffset;
    hud.removeFromSuperViewOnHide = YES;
}
#pragma mark-改变光标
- (void)changeCursor{
    [self.loginBtn addTrackingRect:[self.loginBtn bounds] owner:self userData:nil assumeInside:NO];
    [self.forgetBtn addTrackingRect:[self.forgetBtn bounds] owner:self userData:nil assumeInside:NO];
    [self.quickBtn addTrackingRect:[self.quickBtn bounds] owner:self userData:nil assumeInside:NO];
}
-(void)mouseEntered:(NSEvent *)theEvent{
    NSCursor *cursor = [NSCursor pointingHandCursor];
    [cursor set];
}
- (void)mouseExited:(NSEvent *)theEvent{
    NSCursor *cursor = [NSCursor arrowCursor];
    [cursor set];
}
@end
